package List;

import java.util.ArrayList;
import java.util.Random;

class Card{
    //一张牌
    int rank;//牌的大小
    String suit;//牌的花色

    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "["+
                "数值='" + rank + '\'' +
                ", 花色='" + suit + '\'' +
                "]";
    }
}

class Cards{
    //一副牌
    public static final String[] suits={"♠", "♥","♦","♣"};

    public ArrayList<Card> giveCards() {
        //创建52张牌
        ArrayList<Card> cards = new ArrayList<>();//该花色下对应的 一组牌
        for (int i = 0; i <suits.length ; i++) {
            //将每种花色给13张牌
            for (int j = 1; j <=13; j++) {
                Card card = new Card(j, suits[i]);
                cards.add(card);
            }

        }
        return cards;
    }

    public void shuffle(ArrayList<Card> cards) {
        //洗牌
        for (int i=cards.size()-1;i>0;i--) {
            Random random = new Random();
            int index=random.nextInt(i);//该方法会生成一个 0——i-1的数
            swap(cards,i,index);
        }
    }

    public void swap(ArrayList<Card> cards,int i,int index) {
        Card tmp=cards.get(i);
        cards.set(i,cards.get(index));
        cards.set(index,tmp);
    }

}

public class 扑克牌 {
    public static void main(String[] args) {
        Cards cards = new Cards();
        ArrayList<Card> desk=cards.giveCards();
        System.out.println(desk);
        System.out.println("洗牌后：");
        cards.shuffle(desk);
        System.out.println(desk);

        System.out.println("开始发牌");
        //三个人，轮流发一张牌
        ArrayList<Card> player1 = new ArrayList<>();
        ArrayList<Card> player2 = new ArrayList<>();
        ArrayList<Card> player3 = new ArrayList<>();

        ArrayList<ArrayList<Card>> player = new ArrayList<>();
        player.add(player1);
        player.add(player2);
        player.add(player3);

        //三个人，轮流抓 5 张牌
        for (int i = 0; i <5; i++) {
            for (int j = 0; j <3; j++) {
                Card card = desk.remove(0);
                player.get(j).add(card);
            }
        }

        for (int i=0;i<player.size();i++) {
            System.out.println(player.get(i));
        }

        System.out.println("剩下的牌");
        System.out.println(desk);

    }
}
